﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using Infrastructure.Utilities;

namespace LuckyDraw.WinUI
{
	public partial class SettingForm : Form
	{
		private DesignerSetting setting = null;

		public SettingForm()
		{
			InitializeComponent();

			this.Load += SettingForm_Load;
		}

		void SettingForm_Load(object sender, EventArgs e)
		{
			btnOK.Click += btnOK_Click;
			btnCancel.Click += btnCancel_Click;
			button1.Click += button1_Click;
			button2.Click += button2_Click;
			btnDefault.Click += btnDefault_Click;

			setting = DesignerSetting.Load();
			if (setting == null)
			{
				setting = DefaultSetting;
			}

			InitData();
		}

		public void InitData()
		{
			string[] colorNames = new string[] { 
				Color.White.Name, 
				Color.Red.Name, 
				Color.Black.Name, 
				Color.Blue.Name, 
				Color.Yellow.Name, 
				Color.Green.Name };

			//抽奖次数设置。
			{
				List<string> prizeTypeList = DB.SelectPrizeType();
				textBox8.Text = prizeTypeList[0];
				textBox9.Text = prizeTypeList[1];
				textBox10.Text = prizeTypeList[2];
				textBox11.Text = prizeTypeList[3];
				textBox12.Text = prizeTypeList[4];

				List<Strategy> list = DB.SelectStrategy().Where(a => !a.Name.IsNullOrEmpty()).ToList();

				Strategy strategy = list.FirstOrDefault(a => a.Name == textBox8.Text);
				if (strategy != null)
				{
					numericUpDown5.Value = strategy.Number;
				}

				strategy = list.FirstOrDefault(a => a.Name == textBox9.Text);
				if (strategy != null)
				{
					numericUpDown6.Value = strategy.Number;
				}

				strategy = list.FirstOrDefault(a => a.Name == textBox10.Text);
				if (strategy != null)
				{
					numericUpDown7.Value = strategy.Number;
				}

				strategy = list.FirstOrDefault(a => a.Name == textBox11.Text);
				if (strategy != null)
				{
					numericUpDown8.Value = strategy.Number;
				}

				strategy = list.FirstOrDefault(a => a.Name == textBox12.Text);
				if (strategy != null)
				{
					numericUpDown9.Value = strategy.Number;
				}
			}

			//奖劵号码。
			comboBox1.Items.AddRange(colorNames);
			numericUpDown4.Value = setting.CouponNoX;
			numericUpDown3.Value = setting.CouponNoY;
			textBox3.Text = setting.CouponNoFontName;
			numericUpDown10.Value = (decimal)setting.CouponNoFontSize;
			comboBox1.Text = setting.CouponNoForeColor;

			//抽奖按钮。
			comboBox7.Items.AddRange(colorNames);
			numericUpDown12.Value = setting.DrawButtonX;
			numericUpDown13.Value = setting.DrawButtonY;
			textBox4.Text = setting.DrawButtonFontName;
			numericUpDown11.Value = (decimal)setting.DrawButtonFontSize;
			comboBox7.Text = setting.DrawButtonForeColor;

			//抽奖类型标签。
			comboBox8.Items.AddRange(colorNames);
			numericUpDown15.Value = setting.PrizeTypeLabelX;
			numericUpDown16.Value = setting.PrizeTypeLabelY;
			textBox5.Text = setting.PrizeTypeLabelFontName;
			numericUpDown14.Value = (decimal)setting.PrizeTypeLabelFontSize;
			comboBox8.Text = setting.PrizeTypeLabelForeColor;
			checkBox1.Checked = setting.PrizeTypeLabelVisual;

			//抽奖类型下拉列表框。
			numericUpDown25.Value = setting.PrizeTypeComboX;
			numericUpDown26.Value = setting.PrizeTypeComboY;
			textBox7.Text = setting.PrizeTypeComboFontName;
			numericUpDown24.Value = (decimal)setting.PrizeTypeComboFontSize;

			//获奖名单列表。
			comboBox9.Items.AddRange(colorNames);
			numericUpDown20.Value = setting.WinningX;
			numericUpDown21.Value = setting.WinningY;
			numericUpDown22.Value = setting.WinningWidth;
			numericUpDown23.Value = setting.WinningHeight;
			textBox6.Text = setting.WinningFontName;
			numericUpDown19.Value = (decimal)setting.WinningFontSize;
			comboBox9.Text = setting.WinningForeColor;

			//中奖名单标签。
			comboBox2.Items.AddRange(colorNames);
			numericUpDown28.Value = setting.WinningLabelX;
			numericUpDown29.Value = setting.WinningLabelY;
			textBox13.Text = setting.WinningLabelFontName;
			numericUpDown27.Value = (decimal)setting.WinningLabelFontSize;
			comboBox2.Text = setting.WinningLabelForeColor;
		}

		public DesignerSetting DefaultSetting
		{
			get;
			set;
		}

		void button2_Click(object sender, EventArgs e)
		{
			textBox2.Text = GetPicture();
		}

		//选择底图。
		void button1_Click(object sender, EventArgs e)
		{
			textBox1.Text = GetPicture();
		}

		private string GetPicture()
		{
			openFileDialog1.Multiselect = false;
			openFileDialog1.Filter = "图片文件|*.pnl;*.jpg;*.jpeg;*.bmp|所有文件|*.*";
			if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				return openFileDialog1.FileName;
			}
			return "";
		}

		void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		void btnOK_Click(object sender, EventArgs e)
		{
			setting.BackgroundImage = textBox1.Text;
			setting.LogoImage = textBox2.Text;
			setting.LogoX = (int)numericUpDown1.Value;
			setting.LogoY = (int)numericUpDown2.Value;
			setting.LogoHeight = (int)numericUpDown17.Value;
			setting.LogoWidht = (int)numericUpDown18.Value;

			//奖劵号码。
			setting.CouponNoX = (int)numericUpDown4.Value;
			setting.CouponNoY = (int)numericUpDown3.Value;
			setting.CouponNoFontName = textBox3.Text.Trim();
			setting.CouponNoFontSize = (int)numericUpDown10.Value;
			setting.CouponNoForeColor = comboBox1.Text;

			//抽奖按钮。
			setting.DrawButtonX = (int)numericUpDown12.Value;
			setting.DrawButtonY = (int)numericUpDown13.Value;
			setting.DrawButtonFontName = textBox4.Text.Trim();
			setting.DrawButtonFontSize = (int)numericUpDown11.Value;
			setting.DrawButtonForeColor = comboBox7.Text;

			//抽奖类型标签。
			setting.PrizeTypeLabelX = (int)numericUpDown15.Value;
			setting.PrizeTypeLabelY = (int)numericUpDown16.Value;
			setting.PrizeTypeLabelFontName = textBox4.Text.Trim();
			setting.PrizeTypeLabelFontSize = (int)numericUpDown14.Value;
			setting.PrizeTypeLabelForeColor = comboBox7.Text;
			setting.PrizeTypeLabelVisual = checkBox1.Checked;

			//抽奖类型下拉列表框。
			setting.PrizeTypeComboX = (int)numericUpDown25.Value;
			setting.PrizeTypeComboY = (int)numericUpDown26.Value;
			setting.PrizeTypeComboFontName = textBox7.Text.Trim();
			setting.PrizeTypeComboFontSize = (int)numericUpDown24.Value;

			//获奖名单。
			setting.WinningX = (int)numericUpDown20.Value;
			setting.WinningY = (int)numericUpDown21.Value;
			setting.WinningWidth = (int)numericUpDown22.Value;
			setting.WinningHeight = (int)numericUpDown23.Value;
			setting.WinningFontName = textBox6.Text;
			setting.WinningFontSize = (int)numericUpDown19.Value;
			setting.WinningForeColor = comboBox9.Text;

			//中奖名单标签。
			setting.WinningLabelX = (int)numericUpDown28.Value;
			setting.WinningLabelY = (int)numericUpDown29.Value;
			setting.WinningLabelFontName = textBox13.Text;
			setting.WinningLabelFontSize = (int)numericUpDown27.Value;
			setting.WinningLabelForeColor = comboBox2.Text;

			setting.Save();

			SaveStrategy();

			this.DialogResult = DialogResult.OK;
		}

		private void SaveStrategy()
		{
			List<Strategy> list = new List<Strategy>();

			Strategy strategy1 = new Strategy()
			{
				Id = 1,
				Name = textBox8.Text,
				Number = (int)numericUpDown5.Value
			};
			list.Add(strategy1);

			Strategy strategy2 = new Strategy()
			{
				Id = 2,
				Name = textBox9.Text,
				Number = (int)numericUpDown6.Value
			};
			list.Add(strategy2);

			Strategy strategy3 = new Strategy()
			{
				Id = 3,
				Name = textBox10.Text,
				Number = (int)numericUpDown7.Value
			};
			list.Add(strategy3);

			Strategy strategy4 = new Strategy()
			{
				Id = 4,
				Name = textBox11.Text,
				Number = (int)numericUpDown8.Value
			};
			list.Add(strategy4);

			Strategy strategy5 = new Strategy()
			{
				Id = 5,
				Name = textBox12.Text,
				Number = (int)numericUpDown9.Value
			};
			list.Add(strategy5);

			DB.SaveStrategy(list);
		}

		void btnDefault_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("您确定要恢复控件的默认设置？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				setting = DefaultSetting;
				InitData();
			}
		}
	}
}
